﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

namespace AUV5.Web.Pages
{
    public partial class AddAccount : PageBaseFrontend
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (base.IsVerified)
                {
                    if (!string.IsNullOrEmpty(Request["Id"]))
                    {
                        BindData(Request["Id"]);
                    }
                    else
                    {
                        BindData();
                    }
                }
                else
                {
                    Response.Redirect(URL_INDEX);
                }
            }
        }

        #region##绑定数据
        /// <summary>
        /// 绑定数据
        /// 创建人：Wilson
        /// 创建时间：2012-1-17
        /// </summary>
        private void BindData()
        {
            BindAccountType();
        }

        private void BindData(string id)
        {
            BindAccountType();
        }
        #endregion

        #region##绑定帐号类型
        /// <summary>
        /// 绑定帐号类型
        /// 创建人：Wilson
        /// 创建时间：2012-1-17
        /// </summary>
        private void BindAccountType()
        {
            IList<AUV5.Model.AccountType> list = AUV5.BLL.AccountTypeBLL.GetInstance().GetAccountTypeList();

            rdoListAccountType.DataSource = list;
            rdoListAccountType.DataTextField = "AccountTypeName";
            rdoListAccountType.DataValueField = "AccountTypeID";
            rdoListAccountType.DataBind();

            rdoListAccountType.SelectedIndex = 0;
        }
        #endregion

        #region##提交
        /// <summary>
        /// 提交
        /// 创建人：Wilson
        /// 创建时间：2012-1-17
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSub_Click(object sender, EventArgs e)
        {

            if (txtAccount.Text.Trim() == "")
            {
                Alert("帐号不能为空");
            }
            else if (txtRealName.Text.Trim() == "")
            {
                Alert("真实姓名不能为空");
            }
            else if (txtIDNum.Text.Trim() == "")
            {
                Alert("身份证不能为空");
            }
            else if (txtPhoneNo.Text.Trim() == "")
            {
                Alert("手机号不能为空");
            }
            else
            { 
                Regex idNumRe = new Regex("^(\\d{14}|\\d{17})(\\d|[xX])$", RegexOptions.None);
                Regex phoneRe = new Regex("^1\\d{10}$", RegexOptions.None);
                if (!idNumRe.IsMatch(txtIDNum.Text.Trim()))
                {
                    Alert("身份证号格式不正确");
                }
                else if (!phoneRe.IsMatch(txtPhoneNo.Text.Trim()))
                {
                    Alert("手机号格式不正确");
                }
                else
                {
                    if (!string.IsNullOrEmpty(Request["Id"]))
                    {

                    }
                    else
                    {
                        try
                        {
                            AUV5.Model.UserAccount account = new Model.UserAccount();
                            account.Account = txtAccount.Text.Trim();
                            account.AccountTypeID = rdoListAccountType.SelectedValue;
                            account.IDNum = txtIDNum.Text.Trim();
                            account.PhoneNo = txtPhoneNo.Text.Trim();
                            account.RealName = txtRealName.Text.Trim();
                            account.UserID = VerifiedUserID;
                            account.UserAccountID = Guid.NewGuid().ToString();

                            AUV5.BLL.UserAccountBLL.GetInstance().Add(account);

                            Alert("添加成功");

                            ClearInput();
                        }
                        catch
                        {
                            Alert("添加失败");
                        }
                    }
                }
            }                       
        }
        #endregion

        #region##清空输入
        /// <summary>
        /// 清空输入
        /// 创建人：Wilson
        /// 创建时间：2012-1-17
        /// </summary>
        private void ClearInput()
        {
            txtAccount.Text = "";
            txtIDNum.Text = "";
            txtPhoneNo.Text = "";
            txtRealName.Text = "";
        }
        #endregion
    }

}